package com.lib.image.preview

import android.widget.ImageView
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lib.image.ImageLoader
import com.lib.image.R

class CoverAdapter(private val size: Int): BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder>() {

    private var mOnReplaceClickListener: ((item: CoverDefault, position: Int) -> Unit)? = null
    private var mOnAddClickListener: (()->Unit)? = null

    init {
        addItemType(1, R.layout.layout_item_cover)
        addItemType(2, R.layout.layout_item_cover_add)
    }

    override fun convert(holder: BaseViewHolder, item: MultiItemEntity) {
        val params = holder.itemView.layoutParams
        params.width = size
        params.height = size
        holder.itemView.layoutParams = params
        if(item is CoverAdd) {
            convertAdd(holder, item)
        } else if(item is CoverDefault) {
            convertDefault(holder, item)
        }
    }

    private fun convertDefault(holder: BaseViewHolder, item: CoverDefault) {
        val imageView = holder.getView<ImageView>(R.id.btn_cover)
        ImageLoader.loadCenterCropImage(imageView, item.path, size, size, view = imageView)
        holder.setVisible(R.id.text_select, item.select)
        holder.itemView.setOnClickListener {
            mOnReplaceClickListener?.invoke(item, holder.adapterPosition)
        }
    }

    private fun convertAdd(holder: BaseViewHolder, item: CoverAdd) {
        holder.itemView.setOnClickListener {
            mOnAddClickListener?.invoke()
        }
    }

    fun setOnReplaceClickListener(listener: (item: CoverDefault, position: Int) -> Unit) {
        this.mOnReplaceClickListener = listener
    }

    fun setOnAddClickListener(listener: ()-> Unit) {
        this.mOnAddClickListener = listener
    }


}